package org.je.refl;

import java.lang.reflect.Field;
import java.util.Date;
import org.je.util.DateUtil;

/**
 * 类SpecialClassUtil.java的实现描述： 类实现描述
 * 
 * @author fanyifeng 2012-8-30 下午3:41:46
 */
public class SpecialClassUtil
{
	public static Object createObject(Class<?> cl) {
		if (cl == String.class)
			return "";
		if (cl == Date.class)
			return new Date();
		if (cl.isEnum()) {
			for (Field f : cl.getDeclaredFields()) {
				if (f.getType().isArray() == false) {
					try {
						f.setAccessible(true);
						return f.get(null);
					} catch (Exception ex) {
						throw new RuntimeException("enum-error:class=" + cl.getName() + ", " + ex.getMessage());
					}
				}
			}
		}
		return null;
	}

	public static Object createObject(Class<?> cl, String value) {
		if (cl == String.class)
			return value;
		if (cl == Date.class)
			return DateUtil.parse(value);
		if (cl.isEnum()) {
			try {
				Field field = cl.getDeclaredField(value);
				field.setAccessible(true);
				return field.get(null);
			} catch (Exception e) {
				throw new RuntimeException("enum-error:class=" + cl.getName() + ", " + e.getMessage());
			}
		}
		return null;
	}
}
